// pages/inviteFriendsList/inviteFriendsList.js
import {
    shareOptions
} from "../../utils/access";
import api from "../../config/api";
import request from "../../utils/request";
const { getBarAndTitleHeight } = require("../../utils/utils")
const app = getApp()

Page({
    /**
     * 页面的初始数据
     */
    data: {
        currentTab: 0,
        page: 1,
        total: 0, //总页数
        isDone: false,
        list: [],
        offTotal: 0,
        waitTotal: 0
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.setData({
            // 信号栏高度
            ...getBarAndTitleHeight(),
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
      this.getNum()
      this.getList(1)
    },
    /**
     * 获取列表
     * @param {*} page 当前页
     */
    async getList(page) {
        const res = await request.get(api.inviteNewList, {
            type: this.data.currentTab === 0 ? 2 : 1,
            page,
            size: 10
        }, {
            loading: false
        })
        if (res.httpStatus == 200) {
            this.setData({
                list: page === 1 ? res?.data?.records : [
                    ...this.data.list,
                    ...res?.data?.records,
                ],
                total: res?.data?.pages
            })
        }
        wx.stopPullDownRefresh()
    },
    // 获取邀请数量
    async getNum(){
     let res = await request.get(api.inviteListNumber,{},{loading:false})
     if(res.httpStatus === 200){
         this.setData({
             offTotal: res?.data?.number,
             waitTotal: res?.data?.notNumber
         })
     }
    },
    // tab切换
    switchTab(e) {
        this.setData({
            currentTab: e.detail.current,
            page: 1
        });
        wx.pageScrollTo({
          scrollTop: 0
        })
        this.getNum()
        this.getList(1)
    },
    // 滚动切换标签
    switchNav(e) {
        let cur = e.currentTarget.dataset.current;
        if (this.data.currentTab == cur) {
            return false;
        } else {
            this.setData({
                currentTab: cur
            });
        }
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {
        this.setData({
            page: 1,
            isDone: false,
        })
        this.getNum()
        this.getList(1)
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
        let {
            page,
            total
        } = this.data
        if (page < total) {
            page++
            this.setData({
                page
            })
            this.getNum()
            this.getList(page)
        } else {
            this.setData({
                isDone: true
            })
        }
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function (e) {
        if (app.isLogin()) {
            return shareOptions('newInvite', e)
        } else {
            return shareOptions('default', e, '和我一起养成垃圾分类好习惯', 'pages/inviteFriends/inviteFriends', 'https://image-classify.daieco.com/kejian-quanzi/jPqeBiUylOVBf3a2f19b53e25daca37d49ce974370cb.png')
        }
    }
})